home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mewin10s.zip / FILEIO.C < prev    next >
C/C++ Source or Header  |  1992-01-25  |  6KB  |  283 lines

  1. /*  FILEIO.C:   Low level file i/o routines
  2.         MicroEMACS 3.10
  3.  
  4.  * The routines in this file read and write ASCII files from the disk. All of
  5.  * the knowledge about files are here.
  6.  */
  7.  
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include    "eproto.h"
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #if    AOSVS
  15. #define    fopen    xxfopen
  16. #endif
  17.  
  18. NOSHARE FILE *ffp;        /* File pointer, all functions. */
  19. static int eofflag;        /* end-of-file flag */
  20.  
  21. /*
  22.  * Open a file for reading.
  23.  */
  24. #if !(VMS & RMSIO)    /* if using RMS under VMS, the code */
  25.             /* is in VMS.C */
  26. PASCAL NEAR ffropen(fn)
  27. char    *fn;
  28. {
  29.         if ((ffp=fopen(fn, "r")) == NULL)
  30.                 return(FIOFNF);
  31. #if     WINDOW_MSWIN
  32.         fbusy = FREADING;
  33. #endif
  34.     eofflag = FALSE;
  35.         return(FIOSUC);
  36. }
  37.  
  38. /*
  39.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  40.  * (cannot create).
  41.  */
  42. #if    AOSVS == 0
  43. PASCAL NEAR ffwopen(fn, mode)
  44. char    *fn;
  45. char *mode;    /* mode to open file for */
  46. {
  47.     char xmode[6];        /* extended file open mode */
  48.  
  49.     /* nonstandard line terminators? */
  50.     if (*lterm) {
  51.  
  52.         /* open in binary mode */
  53.         strcpy(xmode, mode);
  54.         strcat(xmode, "b");
  55.         ffp = fopen(fn, xmode);
  56.     } else {
  57.  
  58.         /* open in ascii(text) mode */
  59.         ffp = fopen(fn, mode);
  60.     }
  61.  
  62.         if (ffp == NULL) {
  63.                 mlwrite(TEXT155);
  64. /*                      "Cannot open file for writing" */
  65.                 return(FIOERR);
  66.         }
  67. #if     WINDOW_MSWIN
  68.         fbusy = FWRITING;
  69. #endif
  70.         return(FIOSUC);
  71. }
  72. #endif
  73.  
  74. /*
  75.  * Close a file. Should look at the status in all systems.
  76.  */
  77. PASCAL NEAR ffclose()
  78. {
  79.     /* free this since we do not need it anymore */
  80.     if (fline) {
  81.         free(fline);
  82.         fline = NULL;
  83.     }
  84. #if     WINDOW_MSWIN
  85.         fbusy = FALSE;
  86. #endif
  87. #if    MSDOS & CTRLZ
  88.     putc(26, ffp);        /* add a ^Z at the end of the file */
  89. #endif
  90.     
  91. #if     V7 | USG | SMOS | HPUX | SUN | XENIX | BSD | WMCS | VMS | (MSDOS & (LATTICE | MSC | DTL | TURBO | ZTC)) | OS2 | (TOS & MWC) | AVIION
  92.         if (fclose(ffp) != FALSE) {
  93.                 mlwrite(TEXT156);
  94. /*                      "Error closing file" */
  95.                 return(FIOERR);
  96.         }
  97.         return(FIOSUC);
  98. #else
  99.         fclose(ffp);
  100.         return(FIOSUC);
  101. #endif
  102. }
  103.  
  104. /*
  105.  * Write a line to the already opened file. The "buf" points to the buffer,
  106.  * and the "nbuf" is its length, less the free newline. Return the status.
  107.  * Check only at the newline.
  108.  */
  109. PASCAL NEAR ffputline(buf, nbuf)
  110.  
  111. char    buf[];
  112. int nbuf;
  113.  
  114. {
  115.         register int i;        /* index into line to write */
  116.     register char *lptr;    /* ptr into the line terminator */
  117. #if    CRYPT
  118.     char c;        /* character to translate */
  119.  
  120.     if (cryptflag) {
  121.             for (i = 0; i < nbuf; ++i) {
  122.             c = buf[i];
  123.             crypt(&c, 1);
  124.             putc(c, ffp);
  125.         }
  126.     } else
  127.             for (i = 0; i < nbuf; ++i)
  128.                     putc(buf[i], ffp);
  129. #else
  130.         for (i = 0; i < nbuf; ++i)
  131.                 putc(buf[i], ffp);
  132. #endif
  133.  
  134.     /* write out the appropriate line terminator(s) */
  135.     if (*lterm) {
  136.         lptr = <erm[0];
  137.         while (*lptr)
  138.             putc(*lptr++, ffp);
  139.     } else {
  140.             putc('\n', ffp);
  141.     }
  142.  
  143.     /* check for write errors */
  144.         if (ferror(ffp)) {
  145.                 mlwrite(TEXT157);
  146. /*                      "Write I/O error" */
  147.                 return(FIOERR);
  148.         }
  149.  
  150. #if     WINDOW_MSWIN
  151.         {
  152.             static int  o = 0;
  153.             if (--o < 0) {
  154.                 longop(TRUE);
  155.                 o = 10;    /* to lower overhead, only 10% calls to longop */
  156.             }
  157.         }
  158. #endif
  159.         return(FIOSUC);
  160. }
  161.  
  162. /*
  163.  * Read a line from a file, and store the bytes in the supplied buffer. The
  164.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  165.  * at the end of the file that don't have a newline present. Check for I/O
  166.  * errors too. Return status.
  167.  */
  168. PASCAL NEAR ffgetline()
  169.  
  170. {
  171.         register int c;        /* current character read */
  172.         register int i;        /* current index into fline */
  173.     register char *tmpline;    /* temp storage for expanding line */
  174.  
  175.     /* if we are at the end...return it */
  176.     if (eofflag)
  177.         return(FIOEOF);
  178.  
  179.     /* dump fline if it ended up too big */
  180.     if (flen > NSTRING && fline != NULL) {
  181.         free(fline);
  182.         fline = NULL;
  183.     }
  184.  
  185.     /* if we don't have an fline, allocate one */
  186.     if (fline == NULL)
  187.         if ((fline = malloc(flen = NSTRING)) == NULL)
  188.             return(FIOMEM);
  189.  
  190.     /* read the line in */
  191.         i = 0;
  192.         while ((c = getc(ffp)) != EOF && c != '\n') {
  193.                 fline[i++] = c;
  194.         /* if it's longer, get more room */
  195.                 if (i >= flen) {
  196.             flen *= 2;
  197.             if ((fline = realloc(fline, flen)) == NULL) {
  198.                 return FIOMEM;
  199.             }
  200.                 }
  201.         }
  202.  
  203.     /* dump any extra line terminators at the end */
  204.     while (i > 0 && (fline[i-1] == 10 || fline[i-1] == 13))
  205.         i--;
  206.  
  207.     /* we should be ready to dump leading terminators too - ADD THIS DAN */
  208.  
  209.     /* test for any errors that may have occured */
  210.         if (c == EOF) {
  211.                 if (ferror(ffp)) {
  212.                         mlwrite(TEXT158);
  213. /*                              "File read error" */
  214.                         return(FIOERR);
  215.                 }
  216.  
  217.                 if (i != 0)
  218.             eofflag = TRUE;
  219.         else
  220.             return(FIOEOF);
  221.         }
  222.  
  223.     /* terminate and decrypt the string */
  224.         fline[i] = 0;
  225. #if    CRYPT
  226.     if (cryptflag)
  227.         crypt(fline, strlen(fline));
  228. #endif
  229.         return(FIOSUC);
  230. }
  231. #endif
  232.  
  233. int PASCAL NEAR fexist(fname)    /* does <fname> exist on disk? */
  234.  
  235. char *fname;        /* file to check for existance */
  236.  
  237. {
  238.     FILE *fp;
  239.  
  240.     /* try to open the file for reading */
  241.     fp = fopen(fname, "r");
  242.  
  243.     /* if it fails, just return false! */
  244.     if (fp == NULL)
  245.         return(FALSE);
  246.  
  247.     /* otherwise, close it and report true */
  248.     fclose(fp);
  249.     return(TRUE);
  250. }
  251.  
  252. #if    AZTEC & MSDOS
  253. /*    a1getc:        Get an ascii char from the file input stream
  254.             but DO NOT strip the high bit
  255. */
  256.  
  257. #undef    getc
  258.  
  259. int a1getc(fp)
  260.  
  261. FILE *fp;
  262.  
  263. {
  264.     int c;        /* translated character */
  265.  
  266.     c = getc(fp);    /* get the character */
  267.  
  268.     /* if its a <LF> char, throw it out  */
  269.     while (c == 10)
  270.         c = getc(fp);
  271.  
  272.     /* if its a <RETURN> char, change it to a LF */
  273.     if (c == '\r')
  274.         c = '\n';
  275.  
  276.     /* if its a ^Z, its an EOF */
  277.     if (c == 26)
  278.         c = EOF;
  279.  
  280.     return(c);
  281. }
  282. #endif
  283.